<template>
  <div class="form_2">
    <el-form :model="form" status-icon :rules="rules" ref="ruleForm" label-width="50px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-radio-group v-model="form.role">
          <el-radio label="student">学生</el-radio>
          <el-radio label="teacher">教师</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-button @click="nameByAccount">名称同步账户</el-button>
    </el-form>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";

const emits = defineEmits(['changeCtxFormData'])
const props = defineProps({
  ctxFormData: {
    type: Object,
    default(){
      return {}
    }
  }
})
let name = 'Form2'
let form = ref({
  name: '',
  role: 'student'
})
let ruleForm = ref(null)
let validateName = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('不能为空'));
  }
  callback()
}
let rules = ref({
  name: [
    {validator: validateName, trigger: 'blur'}
  ]
})
let validateCurrent =  () => {
  return new Promise((resolve, reject) => {
    ruleForm.value.validate((valid) => {
      if (valid) resolve()
    })
  })
}
let show = ()=>{
}
let nameByAccount = () => {
  emits('changeCtxFormData', 'name', props.ctxFormData.account)
}

defineExpose({
  form,
  name,
  show,
  validateCurrent
})

</script>

<style scoped>
.form_2 {
  width: 450px;
}
</style>